package com.howen.PageObject;
import com.howen.framework.BasePage;
import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.options.LoadState;
import com.microsoft.playwright.options.WaitForSelectorState;
import com.microsoft.playwright.options.WaitUntilState;


public class LoginPage extends BasePage {
    private final Locator usernameInput;
    private final Locator passwordInput;
    private final Locator loginButton;

    /**
     * 构造函数
     * @param page
     * @param baseUrl
     */
    public LoginPage(Page page, String baseUrl) {
        super(page, baseUrl);
        this.usernameInput = page.locator("//input[@class='el-input__inner' and @type='text']");
        this.passwordInput = page.locator("//input[@class='el-input__inner' and @type='password']");
        this.loginButton = page.locator("'#app > div > div.login-box > div.login-box-from > form > div:nth-child(4) > div > button > span'");
    }
    /**
     * 登录页面
     */
    public void navigateToLogin() {
        String loginUrl = baseUrl + "/vss"; // 确保单斜杠
        System.out.println("Navigating to: " + loginUrl); // 调试输出

        page.navigate(loginUrl, new Page.NavigateOptions()
                .setWaitUntil(WaitUntilState.NETWORKIDLE));

        // 验证关键元素存在
        usernameInput.waitFor(new Locator.WaitForOptions()
                .setState(WaitForSelectorState.ATTACHED));
    }

    /**
     * 登录
     * @param username
     * @param password
     */
    public void login(String username, String password) {
        usernameInput.fill(username);
        passwordInput.fill(password);
        loginButton.click();
    }

    /**
     * 等待页面加载完毕
     */
    private void waitForPageLoad() {
        page.waitForLoadState(LoadState.NETWORKIDLE);
    }
    public String getCurrentUrl() {
        return page.url();
    }

    public boolean isLoginSuccessful() {
        // 根据实际项目实现，例如：
        return page.locator("/vss/#/dashboard").isVisible()
                || page.url().contains("dashboard");
    }
}




